package com.zhangwoo.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 读取配置，填充至静态属性
 * @author chenc
 *
 */
public class Config {

	private static Logger logger = Logger.getLogger(Config.class);
	private static String configFile = ClassLoader.getSystemResource("")
			.toString().replace("file:/", "")+"db_config.ini";// 配置文件
	
	//====================数据库相关配置=======================
	private static String driver;
	private static String url;
	private static String user;
	private static String pwd;

	private static Integer initCon;
	private static Integer maxCon;
	private static Integer incCon;
	private static Integer overtime;
	private static Integer sleeptime;
	
	//====================系统相关配置=======================
	private static String srcpath;
	private static String destpath; 
	
	private static int thcount;// 线程数量 

	private static String hdfshost; // hdfs 服务器IP
	private static String hdfsPath; // hdfs 写入路径
	
	static {
		load();
	}
	
	/**
	 * 从文件中读取属性
	 */
	private static void load(){
		InputStreamReader fis = null;
		try {
			configFile = "/"+configFile;
			logger.info("configFile path is : "+configFile);
			
			fis = new InputStreamReader(new FileInputStream(configFile),"utf-8");
			Properties dbField = new Properties();
			dbField.load(fis);

			driver = dbField.getProperty("driver", "");
			url = dbField.getProperty("url", "");
			user = dbField.getProperty("user", "");
			pwd = dbField.getProperty("password", "");
			initCon = Integer.parseInt(dbField.getProperty("initcon", "3"));
			maxCon = Integer.parseInt(dbField.getProperty("maxcon", "10"));
			incCon = Integer.parseInt(dbField.getProperty("inccon", "3"));
			overtime = Integer.parseInt(dbField.getProperty("overtime", "60"));
			sleeptime = Integer.parseInt(dbField.getProperty("sleeptime", "30"));
			

			hdfshost = dbField.getProperty("hdfshost", "");
			hdfsPath = dbField.getProperty("hdfsPath", "");
		} catch (FileNotFoundException e) {
			logger.error("config/db.ini 配置文件不存在");
		} catch (IOException e) {
			logger.error("读取 config/db.ini 配置文件错误");
		}		
	}
	
	/**
	 * 重新获取属性
	 */
	public static void reload(){
		load();
	}

	public static String getDriver() {
		return driver;
	}

	public static String getUrl() {
		return url;
	}

	public static String getUser() {
		return user;
	}

	public static String getPwd() {
		return pwd;
	}

	public static Integer getInitCon() {
		return initCon;
	}

	public static Integer getMaxCon() {
		return maxCon;
	}

	public static Integer getIncCon() {
		return incCon;
	}

	public static Integer getOvertime() {
		return overtime;
	}

	public static Integer getSleeptime() {
		return sleeptime;
	}
	public static void main(String[] args) {
	}

	public static String getConfigFile() {
		return configFile;
	}

	public static String getSrcpath() {
		return srcpath;
	}

	public static String getDestpath() {
		return destpath;
	}

	public static int getThcount() {
		return thcount;
	}

	public static String getHdfshost() {
		return hdfshost;
	}

	public static String getHdfsPath() {
		return hdfsPath;
	}
}
